package com.ado.bazinga.core.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ado.bazinga.core.annotation.AdminLoginToken;
import com.ado.bazinga.core.annotation.ApiDesc;
import com.ado.bazinga.core.assemble.RoleAssemble;
import com.ado.bazinga.core.common.http.HttpResult;
import com.ado.bazinga.core.common.params.dto.RoleDto;
import com.ado.bazinga.core.common.params.dto.RoleParam;
import com.ado.bazinga.core.common.params.vo.RoleVO;
import com.ado.bazinga.core.dal.entity.Operation;
import com.ado.bazinga.core.dal.entity.Role;
import com.ado.bazinga.core.exception.BazingaException;
import com.ado.bazinga.core.service.OperationService;
import com.ado.bazinga.core.service.RoleService;

/**
 *@author adonais
 *@date 2020年4月7日
 *@description RoleController.java  角色控制器
**/
@RestController
@RequestMapping(value = "/api/role")
public class RoleController {
	
	@Autowired
	@Qualifier(value = "roleService")
	private RoleService roleService ;
	@Autowired
	@Qualifier(value = "roleAssemble")
	private RoleAssemble roleAssemble ;
	@Autowired
	@Qualifier(value = "operationService")
	private OperationService operationService ;
	
	@ApiDesc(desc = "创建角色接口")
	@AdminLoginToken
	@PostMapping(value = "/createRole")
	public HttpResult createRole(@RequestBody RoleDto roleDto) {
		if (StringUtils.isEmpty(roleDto.getRoleName())) {
			throw new BazingaException("角色名不能为空");
		}
		Role role = new Role() ;
		role.setOperationId(roleDto.getOperationId());
		role.setRoleName(roleDto.getRoleName());
		boolean flag = roleService.createRole(role) ;
		if (flag) {
			return HttpResult.ok("创建角色成功") ;
		}
		return HttpResult.error("创建角色失败");
	}
	
	@ApiDesc(desc = "删除角色接口")
	@AdminLoginToken
	@DeleteMapping(value = "/deleteRole")
	public HttpResult deleteRole(String roleId) {
		if(StringUtils.isEmpty(roleId)) {
			throw new BazingaException("角色id不能为空") ;
		}
		boolean flag = roleService.deleteRole(roleId) ;
		if (flag) {
			return HttpResult.ok("删除角色成功") ;
		}
		return HttpResult.error("删除角色失败") ;
	}
	
	@ApiDesc(desc = "按id查询角色接口")
	@AdminLoginToken
	@GetMapping(value = "/getRoleById")
	public HttpResult getRoleById(String roleId) {
		if(StringUtils.isEmpty(roleId)) {
			throw new BazingaException("角色id不能为空") ;
		}
		Role role = roleService.findByRoleId(roleId) ;
		if (role == null) {
			return HttpResult.error(421, "查无该角色信息") ;
		}
		RoleVO roleVO = new RoleVO() ;
		roleVO.setId(role.getId().toString());
		roleVO.setRoleName(role.getRoleName());
		if (StringUtils.isEmpty(role.getOperationId())) {
			roleVO.setOps(null);
		}
		List<Operation> operations = getOperations(role.getOperationId()) ;
		roleVO.setOps(operations);
		return HttpResult.ok(roleVO) ;
	}
	
	@ApiDesc(desc = "修改角色接口")
	@AdminLoginToken
	@PostMapping(value = "/updateRole")
	public HttpResult updateRole(@RequestBody RoleParam roleParam) {
		if(StringUtils.isEmpty(roleParam.getId())) {
			throw new BazingaException("角色id不能为空") ;
		}
		Role role = new Role() ;
		role = assembleToRole(role, roleParam) ;
		boolean flag = roleService.updateRole(role) ;
		if (flag) {
			return HttpResult.ok("修改角色成功") ;
		}
		return HttpResult.error("修改角色失败") ;
	}
	
	@ApiDesc(desc = "查询所有角色接口")
	@AdminLoginToken
	@GetMapping(value = "/getAllRoles")
	public HttpResult getAllRoles() {
		List<Role> roles = roleService.getAllRoles() ;
		if (roles.size() < 1) {
			return HttpResult.error(421, "无角色信息");
		}
		List<RoleVO> roleVOs = new ArrayList<RoleVO>();
		for (Role role : roles) {
			RoleVO roleVO = new RoleVO() ;
			roleVO.setId(role.getId().toString());
			roleVO.setRoleName(role.getRoleName());
			List<Operation> ops = getOperations(role.getOperationId()) ;
			roleVO.setOps(ops);
			roleVOs.add(roleVO) ;
		}
		return HttpResult.ok(roleVOs) ;
	}
	
	
	public List<Operation> getOperations(String operationStr) {
		List<Operation> operations = new ArrayList<Operation>() ;
		if (operationStr.length()  == 1) {
			Operation operation = operationService.findById(operationStr) ;
			operations.add(operation) ;
		} else if (operationStr.length() > 1) {
			String[] operaArr= operationStr.split(",") ;
			for (int i = 0 ; i < operaArr.length ; i++) {
				Operation operation = operationService.findById(operaArr[i]) ;
				operations.add(operation) ;
			}
		}
		return operations ;
	}
	
	public Role assembleToRole(Role role , RoleParam roleParam) {
		role.setId(Integer.valueOf(roleParam.getId()));
		role.setRoleName(roleParam.getRoleName());
		role.setOperationId(roleParam.getOperationId());
		return role ;
	}

}
